/*
 * Created by SharpDevelop.
 * User: michaelo
 * Date: 18/05/2005
 * Time: 19:01
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Xml.Schema;
using Orz.LogManager.Config;
using Orz.Gui.Menus;

namespace Orz.LogManager
{
	
	
/// <summary>
/// Description of LogsetManagerForm.
/// </summary>
public class LogsetManagerForm : System.Windows.Forms.Form
{
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.Button cmdEdit;
		private System.Windows.Forms.Label lblMain;
		private System.Windows.Forms.LinkLabel lnkMRU4;
		private System.Windows.Forms.LinkLabel lnkMRU2;
		private System.Windows.Forms.LinkLabel lnkMRU3;
		private System.Windows.Forms.LinkLabel lnkMRU1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.ListView lvwLogsets;
		private System.Windows.Forms.Button cmdExplore;
		private System.Windows.Forms.Button cmdDelete;
		private System.Windows.Forms.Button cmdCancel;
		private System.Windows.Forms.ColumnHeader colDescription;
		private System.Windows.Forms.ColumnHeader colName;
		private System.Windows.Forms.Button cmdOpen;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button cmdNew;
	

	
	private IList logsets_i;
	private LogsetWithFile selectedLogset_i;
	
	
	
	
	private HelpForm help_i;
	
	
	public LogsetManagerForm()
	{
		//
		// The InitializeComponent() call is required for Windows Forms designer support.
		//
		InitializeComponent();
		
		help_i = new HelpForm (this);
				
		try
		{
			logsets_i = LogsetLoader.GetLogsets();		
			PopulateLogsetList (logsets_i, 0);
			PopulateMRUList();
		}
		catch (Exception e)
		{
			MessageBox.Show (e.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
		}
		
		lvwLogsets.Select();
	}
	
	
	private void OpenSelectedLogset()
	{
		if (0 == lvwLogsets.Items.Count  ||  0 == lvwLogsets.SelectedIndices.Count) return;
		
		int iIndex = lvwLogsets.SelectedIndices[0];
		selectedLogset_i = (LogsetWithFile) logsets_i[iIndex];
		this.Close();	
	}
	
	
	
	
	public LogsetWithFile SelectedLogset
	{
		get { return selectedLogset_i; }
	}
	
	
	
	#region Windows Forms Designer generated code
	/// <summary>
	/// This method is required for Windows Forms designer support.
	/// Do not change the method contents inside the source code editor. The Forms designer might
	/// not be able to load this method if it was changed manually.
	/// </summary>
	private void InitializeComponent() {
		System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(LogsetManagerForm));
		this.cmdNew = new System.Windows.Forms.Button();
		this.label1 = new System.Windows.Forms.Label();
		this.cmdOpen = new System.Windows.Forms.Button();
		this.colName = new System.Windows.Forms.ColumnHeader();
		this.colDescription = new System.Windows.Forms.ColumnHeader();
		this.cmdCancel = new System.Windows.Forms.Button();
		this.cmdDelete = new System.Windows.Forms.Button();
		this.cmdExplore = new System.Windows.Forms.Button();
		this.lvwLogsets = new System.Windows.Forms.ListView();
		this.groupBox1 = new System.Windows.Forms.GroupBox();
		this.lnkMRU1 = new System.Windows.Forms.LinkLabel();
		this.cmdEdit = new System.Windows.Forms.Button();
		this.lblMain = new System.Windows.Forms.Label();
		this.lnkMRU2 = new System.Windows.Forms.LinkLabel();
		this.lnkMRU3 = new System.Windows.Forms.LinkLabel();
		this.lnkMRU4 = new System.Windows.Forms.LinkLabel();
		this.groupBox1.SuspendLayout();
		this.SuspendLayout();
		// 
		// cmdNew
		// 
		this.cmdNew.Location = new System.Drawing.Point(224, 440);
		this.cmdNew.Name = "cmdNew";
		this.cmdNew.Size = new System.Drawing.Size(75, 24);
		this.cmdNew.TabIndex = 2;
		this.cmdNew.Tag = "Creates a new configuration file and allows you to add log files to it.";
		this.cmdNew.Text = "&New...";
		this.cmdNew.Click += new System.EventHandler(this.CmdNewClick);
		// 
		// label1
		// 
		this.label1.Location = new System.Drawing.Point(16, 56);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(100, 23);
		this.label1.TabIndex = 3;
		this.label1.Text = "Recently used:";
		// 
		// cmdOpen
		// 
		this.cmdOpen.DialogResult = System.Windows.Forms.DialogResult.OK;
		this.cmdOpen.Location = new System.Drawing.Point(504, 16);
		this.cmdOpen.Name = "cmdOpen";
		this.cmdOpen.Size = new System.Drawing.Size(75, 24);
		this.cmdOpen.TabIndex = 2;
		this.cmdOpen.Tag = "Opens all the log files in the selected configuration file.";
		this.cmdOpen.Text = "&Open";
		this.cmdOpen.Click += new System.EventHandler(this.CmdOpenClick);
		// 
		// colName
		// 
		this.colName.Text = "Configuration file";
		this.colName.Width = 180;
		// 
		// colDescription
		// 
		this.colDescription.Text = "Description";
		this.colDescription.Width = 245;
		// 
		// cmdCancel
		// 
		this.cmdCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
		this.cmdCancel.Location = new System.Drawing.Point(504, 48);
		this.cmdCancel.Name = "cmdCancel";
		this.cmdCancel.Size = new System.Drawing.Size(75, 24);
		this.cmdCancel.TabIndex = 2;
		this.cmdCancel.Tag = "Closes Log Manager.";
		this.cmdCancel.Text = "&Cancel";
		this.cmdCancel.Click += new System.EventHandler(this.CmdCloseClick);
		// 
		// cmdDelete
		// 
		this.cmdDelete.Location = new System.Drawing.Point(400, 440);
		this.cmdDelete.Name = "cmdDelete";
		this.cmdDelete.Size = new System.Drawing.Size(75, 24);
		this.cmdDelete.TabIndex = 2;
		this.cmdDelete.Tag = "Deletes the selected configuration file.";
		this.cmdDelete.Text = "&Delete...";
		this.cmdDelete.Click += new System.EventHandler(this.CmdDeleteClick);
		// 
		// cmdExplore
		// 
		this.cmdExplore.Location = new System.Drawing.Point(504, 88);
		this.cmdExplore.Name = "cmdExplore";
		this.cmdExplore.Size = new System.Drawing.Size(75, 24);
		this.cmdExplore.TabIndex = 2;
		this.cmdExplore.Tag = "Lets you open a configuration file using Windows Explorer.";
		this.cmdExplore.Text = "&Explore...";
		this.cmdExplore.Click += new System.EventHandler(this.CmdExploreClick);
		// 
		// lvwLogsets
		// 
		this.lvwLogsets.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
						this.colName,
						this.colDescription});
		this.lvwLogsets.FullRowSelect = true;
		this.lvwLogsets.GridLines = true;
		this.lvwLogsets.HideSelection = false;
		this.lvwLogsets.Location = new System.Drawing.Point(16, 160);
		this.lvwLogsets.MultiSelect = false;
		this.lvwLogsets.Name = "lvwLogsets";
		this.lvwLogsets.Size = new System.Drawing.Size(456, 264);
		this.lvwLogsets.TabIndex = 0;
		this.lvwLogsets.Tag = resources.GetString("lvwLogsets.Tag");
		this.lvwLogsets.UseCompatibleStateImageBehavior = false;
		this.lvwLogsets.View = System.Windows.Forms.View.Details;
		this.lvwLogsets.DoubleClick += new System.EventHandler(this.LvwLogsetsDoubleClick);
		// 
		// groupBox1
		// 
		this.groupBox1.Controls.Add(this.lnkMRU1);
		this.groupBox1.Controls.Add(this.label1);
		this.groupBox1.Controls.Add(this.lvwLogsets);
		this.groupBox1.Controls.Add(this.cmdDelete);
		this.groupBox1.Controls.Add(this.cmdEdit);
		this.groupBox1.Controls.Add(this.cmdNew);
		this.groupBox1.Controls.Add(this.lblMain);
		this.groupBox1.Controls.Add(this.lnkMRU2);
		this.groupBox1.Controls.Add(this.lnkMRU3);
		this.groupBox1.Controls.Add(this.lnkMRU4);
		this.groupBox1.Location = new System.Drawing.Point(8, 7);
		this.groupBox1.Name = "groupBox1";
		this.groupBox1.Size = new System.Drawing.Size(488, 473);
		this.groupBox1.TabIndex = 3;
		this.groupBox1.TabStop = false;
		// 
		// lnkMRU1
		// 
		this.lnkMRU1.Location = new System.Drawing.Point(120, 56);
		this.lnkMRU1.Name = "lnkMRU1";
		this.lnkMRU1.Size = new System.Drawing.Size(344, 23);
		this.lnkMRU1.TabIndex = 4;
		this.lnkMRU1.TabStop = true;
		this.lnkMRU1.Text = "Test";
		this.lnkMRU1.VisitedLinkColor = System.Drawing.Color.Blue;
		this.lnkMRU1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.LnkMRULinkClicked);
		// 
		// cmdEdit
		// 
		this.cmdEdit.Location = new System.Drawing.Point(312, 440);
		this.cmdEdit.Name = "cmdEdit";
		this.cmdEdit.Size = new System.Drawing.Size(75, 24);
		this.cmdEdit.TabIndex = 2;
		this.cmdEdit.Tag = "Displays details of all the individual log files referenced by the selected confi" +
		"guration file, and lets you edit them or add new ones.";
		this.cmdEdit.Text = "&Modify...";
		this.cmdEdit.Click += new System.EventHandler(this.CmdEditClick);
		// 
		// lblMain
		// 
		this.lblMain.Location = new System.Drawing.Point(16, 22);
		this.lblMain.Name = "lblMain";
		this.lblMain.Size = new System.Drawing.Size(456, 30);
		this.lblMain.TabIndex = 0;
		this.lblMain.Text = "Select a set of log files to view, or click \'New...\' to create a new configuratio" +
		"n.";
		// 
		// lnkMRU2
		// 
		this.lnkMRU2.Location = new System.Drawing.Point(120, 80);
		this.lnkMRU2.Name = "lnkMRU2";
		this.lnkMRU2.Size = new System.Drawing.Size(344, 23);
		this.lnkMRU2.TabIndex = 4;
		this.lnkMRU2.TabStop = true;
		this.lnkMRU2.Text = "Test";
		this.lnkMRU2.VisitedLinkColor = System.Drawing.Color.Blue;
		this.lnkMRU2.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.LnkMRULinkClicked);
		// 
		// lnkMRU3
		// 
		this.lnkMRU3.Location = new System.Drawing.Point(120, 104);
		this.lnkMRU3.Name = "lnkMRU3";
		this.lnkMRU3.Size = new System.Drawing.Size(344, 23);
		this.lnkMRU3.TabIndex = 4;
		this.lnkMRU3.TabStop = true;
		this.lnkMRU3.Text = "Test";
		this.lnkMRU3.VisitedLinkColor = System.Drawing.Color.Blue;
		this.lnkMRU3.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.LnkMRULinkClicked);
		// 
		// lnkMRU4
		// 
		this.lnkMRU4.Location = new System.Drawing.Point(120, 128);
		this.lnkMRU4.Name = "lnkMRU4";
		this.lnkMRU4.Size = new System.Drawing.Size(344, 23);
		this.lnkMRU4.TabIndex = 4;
		this.lnkMRU4.TabStop = true;
		this.lnkMRU4.Text = "Test";
		this.lnkMRU4.VisitedLinkColor = System.Drawing.Color.Blue;
		this.lnkMRU4.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.LnkMRULinkClicked);
		// 
		// LogsetManagerForm
		// 
		this.AcceptButton = this.cmdOpen;
		this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
		this.CancelButton = this.cmdCancel;
		this.ClientSize = new System.Drawing.Size(586, 490);
		this.Controls.Add(this.groupBox1);
		this.Controls.Add(this.cmdOpen);
		this.Controls.Add(this.cmdCancel);
		this.Controls.Add(this.cmdExplore);
		this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
		this.MaximizeBox = false;
		this.MinimizeBox = false;
		this.Name = "LogsetManagerForm";
		this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
		this.Text = "Log Manager Configurations";
		this.Load += new System.EventHandler(this.LogsetManagerFormLoad);
		this.Click += new System.EventHandler(this.LogsetManagerFormClick);
		this.groupBox1.ResumeLayout(false);
		this.ResumeLayout(false);
		}
	#endregion
	void CmdOpenClick(object sender, System.EventArgs e)
	{
		OpenSelectedLogset();
	}
	
	void CmdCloseClick(object sender, System.EventArgs e)
	{
		selectedLogset_i = null;
		this.Close();
	}
	
	void CmdNewClick(object sender, System.EventArgs e)
	{
		LogsetWithFile set = new LogsetWithFile();	
		
		if (lvwLogsets.Items.Count > 0  &&  1 == lvwLogsets.SelectedIndices.Count)
		{
			int iIndex = lvwLogsets.SelectedIndices[0];
			LogsetWithFile selected = (LogsetWithFile) logsets_i[iIndex];
			
			if (DialogResult.Yes == MessageBox.Show ("Okey dokey!  Do you want to create a new configuration based on the selected one (" + selected.FileName + ")?", "New configuration", MessageBoxButtons.YesNo))
			{
				set = LogsetLoader.CloneLogsetWithFile(selected);
				set.FileName = null;
			}
		}
		
		EditLogsetForm f = new EditLogsetForm (set);
		f.ShowDialog();
		
		if (f.WasCancelled) return;
		
		logsets_i.Add (set);
//		AddLogsetToList (set);				
		
		PopulateLogsetList(logsets_i, logsets_i.Count-1);
		
		
	}
	
	
	
	void CmdEditClick(object sender, System.EventArgs e)
	{
		if (0 == lvwLogsets.Items.Count  ||  0 == lvwLogsets.SelectedIndices.Count) return;
		
		int iIndex = lvwLogsets.SelectedIndices[0];
		LogsetWithFile lwf = (LogsetWithFile) logsets_i[iIndex];
		
		EditLogsetForm f = new EditLogsetForm (lwf);
		f.ShowDialog();
		
		if (f.WasCancelled) return;
		
		PopulateLogsetList(logsets_i, iIndex);
		
	}
	
	void CmdDeleteClick(object sender, System.EventArgs e)
	{
		if (0 == lvwLogsets.Items.Count  ||  0 == lvwLogsets.SelectedIndices.Count) return;
		
		int iIndex = lvwLogsets.SelectedIndices[0];
		LogsetWithFile lwf = (LogsetWithFile) logsets_i[iIndex];
		
		if (DialogResult.Yes == MessageBox.Show("Are you sure you want to delete the configuration " + lwf.FileName + "?", this.Text, MessageBoxButtons.YesNo))
		{
			logsets_i.Remove(lwf);
			
			LogsetLoader.DeleteLogset(lwf.FileName);
			PopulateLogsetList(logsets_i, 0);
		}
		
	}
	
	
	
	void LogsetManagerFormLoad(object sender, System.EventArgs e)
	{
		
	}
	
	
	
	private void PopulateMRUList()
	{
		try
		{
		MRUList mruList = new MRUList("Orz", "Log Manager", null);
		
		LinkLabel[] links = { lnkMRU1, lnkMRU2, lnkMRU3, lnkMRU4 };
		
		for (int i=0; i<4; i++)
		{
			if (mruList.FileList.Count > i)
			{
				links[i].Tag = mruList.FileList[i];
				links[i].Text = (string) mruList.FileList[i];
				links[i].Enabled = true;
			}
			else
			{
				links[i].Text = "(none)";
				links[i].Enabled = false;
			}
		}	
		}
		catch (Exception ex)
		{
			MessageBox.Show(ex.Message + "\n" + ex.StackTrace);
		}
	}
	
	
	private void PopulateLogsetList (IList logsets_p, int iSelectedItemIndex_p)
	{
		lvwLogsets.Items.Clear();
		
		IEnumerator e = logsets_p.GetEnumerator();
		
		while (e.MoveNext())
		{
			LogsetWithFile l = (LogsetWithFile) e.Current;
			AddLogsetToList (l);
		}	
		
		if (lvwLogsets.Items.Count > 0)
		{
			lvwLogsets.SelectedItems.Clear();
			
			if (iSelectedItemIndex_p >= 0  &&  iSelectedItemIndex_p < lvwLogsets.Items.Count)
			{
				ListViewItem itemToSelect = lvwLogsets.Items[iSelectedItemIndex_p];
				itemToSelect.Selected = true;
				itemToSelect.EnsureVisible();				
			}		
		}
		
		lvwLogsets.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
		
		UpdateButtons();
		
		lvwLogsets.Select();
	}
	
	
	private void AddLogsetToList (LogsetWithFile set_p)
	{	
		String sName = set_p.FileName;
		
		ListViewItem item =  lvwLogsets.Items.Add(sName, 0);		
		
		if (null != set_p.Logset.description)
		{						
			item.SubItems.Add(set_p.Logset.description);
		}		
	}
	
	
	
	private void UpdateButtons()
	{
		bool bLogsetsExist = (lvwLogsets.Items.Count > 0);
		
		
		cmdOpen.Enabled = bLogsetsExist;
		cmdDelete.Enabled = bLogsetsExist;
		cmdEdit.Enabled = bLogsetsExist;
	}
	
	void LogsetManagerFormClick(object sender, System.EventArgs e)
	{
		
	}
	
	void LvwLogsetsDoubleClick(object sender, System.EventArgs e)
	{
		OpenSelectedLogset();
	}

	
	void CmdExploreClick (object sender, System.EventArgs e)
	{
		OpenFileDialog oDialog = new OpenFileDialog();
		
		oDialog.Filter = "Configuration files (*.xml)|*.xml|All files (*.*)|*.*";
		oDialog.FilterIndex = 1;
		oDialog.Multiselect = false;
		oDialog.InitialDirectory = Application.StartupPath;
		
		if (DialogResult.OK == oDialog.ShowDialog())
		{
			string sConfigFileName = oDialog.FileName;

			try
			{
				selectedLogset_i = LogsetLoader.LoadLogsetFromFile (sConfigFileName);						
				this.Close();
			}
			catch (Exception ex)
			{
				MessageBox.Show ("Couldn't open configuration file " + sConfigFileName);
			}			
		}
	}
	
	
	
	void LnkMRULinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
	{
		
		
		LinkLabel link = (LinkLabel) sender;
		
		
		selectedLogset_i = LogsetLoader.LoadLogsetFromFile((string) link.Tag);
		this.Close();	
	}

}
}
